/**
 * @file name TBTaskActivity.java
 */
package com.digital.community;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.digital.community.common.bean.CommonBean;
import com.digital.community.common.bean.Task;
import com.digital.community.common.utils.ConnectWeb;
import com.digital.community.common.utils.HttpUtils;
import com.digital.community.common.utils.UnitsUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author yarine.gao
 * @date 2012-11-8
 * @version 1.0.0
 */

public class TBTaskDetailActivity extends Activity {
	private ProgressDialog proDialog;
	private Context mContext;
	private String result;
	Message msg;
	private JSONObject jsonObj;
	private TextView taskNoText;
	private TextView bigTypeText;
	private TextView smallTypeText;
	private TextView taskDetailText;
	private Button bt_submit;
	CommonBean commonBean;
	private String statue;
	ArrayAdapter<String> adapter;
	private Spinner spinner;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			proDialog.cancel();
			switch(msg.what){
			case 1:
				try {
					taskNoText.setText(jsonObj.getString("tno"));
					bigTypeText.setText(jsonObj.getString("tbtype"));
					smallTypeText.setText(jsonObj.getString("tstype"));
					taskDetailText.setText(jsonObj.getString("tdetail"));
					
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					Toast.makeText(mContext, "载入失败！", Toast.LENGTH_SHORT).show();
					finish();
				}
				break;
			case 2:
				Toast.makeText(mContext, "载入失败！", Toast.LENGTH_SHORT).show();
				finish();
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		commonBean  = (CommonBean)getApplicationContext();
		setContentView(R.layout.tbtask_detail);
		initView();
	}
	
	public void onResume(){
		super.onResume();
		String taskString = getIntent().getStringExtra("task");
		System.out.println(taskString);
		try {
			jsonObj = new JSONObject(taskString);
			statue = jsonObj.getString("tstatus");
			sendMessage(1);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(statue.equals("1")){
			bt_submit.setClickable(false);
		}else if(statue.equals("2")){
			adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[]{"任务代办"});
			statue = "3";
		}else if(statue.equals("3")){
			adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[]{"拟完成"});
			statue = "5";
		}else if(statue.equals("4")){
			adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[]{"监督回访"});
			statue = "5";
		}else if(statue.equals("5")){
			adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[]{"完成任务","退回任务"});
			spinner.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					
				}
			});
			statue = "3";
		}
		spinner.setAdapter(adapter);
	}
	
	public void setReback(View v){
		if(!statue.equals("")){
			new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Task task = new Task();
				try {
					task.setTno(jsonObj.getString("tno"));
					task.setTstatus(jsonObj.getString("tno"));
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				//更改状态
				new ConnectWeb().httpPost(HttpUtils.DEFAULT_URL+"task/getNewTask.do","task",new Gson().toJson(task).toString());
			}
		}).start();
		}
	}
	
	public void getAttach(View view){
		try {
			String attacks = jsonObj.getString("tattach");
			Intent intent = new Intent();
			intent.setClass(mContext, TBGetAttach.class);
			intent.putExtra("tattach", attacks);
			startActivity(intent);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void initView(){
		proDialog = new ProgressDialog(this);
		proDialog.setMessage("正在加载……");
		bt_submit = (Button) findViewById(R.id.bt_submit);
		taskNoText = (TextView) findViewById(R.id.txt_name);
		bigTypeText = (TextView) findViewById(R.id.sp_big_category);
		smallTypeText = (TextView) findViewById(R.id.sp_small_category);
		taskDetailText = (TextView) findViewById(R.id.sp_nation);
		spinner = (Spinner) findViewById(R.id.sp_reback);
	}
	
    public void sendMessage(int what){
    	msg = handler.obtainMessage();
		msg.what=what;
		handler.sendMessage(msg);
    }

}
